package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.zip.DataFormatException;

@Service
@Slf4j
public class DishService implements com.sky.service.DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //开启事物
    @Transactional
    public void adddish(DishDTO dishdto) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishdto, dish);

        //插入一条数据到菜品表
        dishMapper.insert(dish);
        //插入了以后就有id了
        Long id = dish.getId();

        //插入数据到口味表
        List<DishFlavor> flavors = dishdto.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach( dishFlavor->dishFlavor.setId(id));
            dishFlavorsMapper.insert(flavors);
        }



    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void deletebatch(List<Long> ids) throws DataFormatException {
        //判断是否起售
        for (Long id : ids) {
            Dish dish=dishMapper.getbyId(id);
            Integer status = dish.getStatus();
            if(status== StatusConstant.ENABLE){
                throw new DataFormatException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断是否关联套餐
        List<Long> setmealid= setmealDishMapper.getsetmealidbydishid(ids);
        if(setmealid!=null&&setmealid.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);

        }
       /* //删除菜品表数据
        for (Long id : ids) {
            dishMapper.deletebyid(id);
            //根据菜品id删除口味表
            dishFlavorsMapper.deletebydishid(id);

        }
        //删除口味表数据*/

        dishMapper.deletebyids(ids);

        dishFlavorsMapper.deletebydishids(ids);


    }

    @Override
    public DishVO getdishbyid(Long id) {
        Dish dish = dishMapper.getbyId(id);
        List<DishFlavor> list=dishFlavorsMapper.getbydishid(id);

        DishVO dishvo=new DishVO();
        BeanUtils.copyProperties(dish,dishvo);
        dishvo.setFlavors(list);
        return dishvo;

    }
    @Transactional
    @Override
    public void update(DishDTO dishdto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishdto, dish);
        dishMapper.update(dish);
        dishFlavorsMapper.deletebydishid(dishdto.getId());
        List<DishFlavor> flavors = dishdto.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(new Consumer<DishFlavor>() {
                @Override
                public void accept(DishFlavor dishFlavor) {
                    dishFlavor.setDishId(dishdto.getId());
                }
            });
            dishFlavorsMapper.insert(flavors);
        }


    }

    @Override
    public List<Dish> getdishbycategoryid(Long categoryid) {
        //构建一个查询对象,这样在mybaist中得到是满足dish这个对象的所以对象集合
        Dish dish = Dish.builder().categoryId(categoryid).status(StatusConstant.ENABLE).build();
        List<Dish> list= dishMapper.list(dish);
        return list;

    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.getbydishid(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
